package com.gopay.scheduler.job;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.common.domain.R;
import com.gopay.common.util.FastJsonUtil;
import com.gopay.sms.client.GopaySMSHttpClient;
import com.gopay.sms.common.constants.SMSAppId;
import com.gopay.sms.common.domain.RequestVO;
import com.gopay.sms.common.domain.ResponseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.util.CalendarUtils;
import com.gopay.remote.cps.ic.InnerCheckRemoteService;
import com.gopay.scheduler.GopayBaseJob;

/**
 * 成功订单校验定时任务
 * @IcOrderDailyJob.java
 * @author liuyu
 * @2016年12月25日 下午2:00:00  www.gopay.com.cn Inc.All rights reserved.
 */
public class IcSuccessOrderJob extends GopayBaseJob {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Resource(name = "InnerCheckRemoteService")
    private InnerCheckRemoteService innerCheckRemoteService;

    private static final String SWITCH_ON = "on";
    @Resource(name = "smsConfig")
    private Map<String, String> smsConfig;

    @Override
    protected void doJobInternal() {
        try {
            logger.info("【内部对账——成功订单金额校验】开始！");
            Date date = CalendarUtils.getNextDate(new Date(), -1);
            R r = innerCheckRemoteService.checkSuccessOrderAndAccounting(false, date);
            logger.info("【内部对账——成功订单金额校验】任务返回结果：{}" , FastJsonUtil.toJSONString(r));
            int code = (int)r.get("code");
            if (code == 0){
                //有入库数据，需要发送短信报警
                int orderMore = (int) r.get("01");
                int transMore = (int) r.get("02");
                StringBuilder sb = new StringBuilder();
                sb.append("内部对账成功订单校验发现问题，其中订单长款笔数：");
                sb.append(orderMore);
                sb.append("；账务长款笔数：");
                sb.append(transMore);
                sb.append("。请及时处理！环境：");
                sb.append(smsConfig.get("param"));
                logger.info("【内部对账——成功订单金额校验】准备发送短信报警，内容：{}" , sb.toString());
                sendSms(sb.toString());
            }
            logger.info("【内部对账——成功订单金额校验】结束！");
        } catch (Exception e) {
            logger.error("【内部对账——成功订单金额校验】出错！", e);
        }
    }

    private void sendSms(String message) {
        try {
            if (SWITCH_ON.equals(smsConfig.get("switch"))) {
                // 自定义短信内容，不提供验证码校验功能
                RequestVO req = new RequestVO();
                req.setAppId(SMSAppId._90000.value);
                // 电话号码(多个以逗号分隔)
                req.setMobile(smsConfig.get("mobile"));
                req.setContent(message);
                ResponseVO resp = GopaySMSHttpClient.getInstance().createVerificationCode(req);
                if (resp != null) {
                    if ("100".equals(resp.getStatusCode())) {
                        logger.error("【内部对账——成功订单金额校验】预警短信发送成功");
                    } else {
                        logger.error("【内部对账——成功订单金额校验】预警短信发送失败，原因：" + resp.getMessage());
                    }
                }
            } else {
                logger.error("【内部对账——成功订单金额校验】cron 预警短信发送开关已关闭.");

            }
        } catch (Exception e) {
            logger.error("【内部对账——成功订单金额校验】短信发送发生未知异常", e);
        }
    }
}
